<?
/**
* Create the minimalistic connection
* And data tranfer to oracleDB
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @copyright Copyright (c)2007, Adesanto Asman
* @package oracleDb
*
*/

/**
 * _IS_SILENT and _IS_DEBUG
 * 1 : _IS_DEBUG
 * 0 : _IS_SILENT
 *
 * @see Misc.php
 *
 */
define("_DEBUG_OPTION"                        , 0);

define("_FIELD_TYPE_STRING"                   , "STRING");
define("_FIELD_TYPE_NUMBER"                   , "NUMBER");

/**
* All vars of classOracleDb are define here
* @package dbVars
*/
class dbVars{

  /**
  * @var string
  */
  var $dbConn;

  /**
  * @var integer
  */
  var $nrows;

  /**
  * @var array
  */
  var $result;

  /**
  * @var string you can leave description or note here
  */
  var $userComment;

  /**
  * @var string
  */
  var $userName;

  /**
  * @var string
  */
  var $userPwd;

  /**
  * @var string
  */
  var $userSid;

  /**
  * @var string
  */
  var $query;

  /**
  * @var boolean
  */
  var $status;

  /**
   * @var array   list of string of field' type
   */
  public $lstString = array("VARCHAR",
                            "VARCHAR2");
}

/**
* Extends dbVars
* @package oracleDb
* @see dbVars
*/
class oracleDb extends dbVars{

  /**
  * Init all necessary variables
  * @param string $userName
  * @param string $userPwd
  * @param string $userSid
  * @param string $userComment
  *
  */
  function init($userName,
                $userPwd,
                $userSid,
                $userComment){

    $this->userName     = $userName;
    $this->userPwd      = $userPwd;
    $this->userSid      = $userSid;
    $this->userComment  = $userComment;
  }

  /**
  * It returns false if fail
  * @param void
  * @return boolean
  *
  */
  function connect(){

    //PutEnv("TNS_ADMIN=C:\\oraclexe\\app\\oracle\\product\\10.2.0\\server\\NETWORK\\ADMIN");
    if($dbConn = OCILogon($this->userName, $this->userPwd, $this->userSid)){

      $this->dbConn = $dbConn;
      return true;
    }else{

      return false;
    }
  }

  /**
  * Disconnect the DB
  * @param void
  *
  */
  function dc(){

    OCILogoff($this->dbConn);
  }

  /**
   * translate the field name
   * into easy type
   *
   * @param string $srcType
   * @return string
   */
  function easyType($srcType){

    $max      = sizeof($this->lstString);

    for($idx = 0; $idx < $max; $idx++){

      $ret = preg_match("/".$this->lstString[$idx]."/i",
                        $srcType);

      if($ret > 0){

        return _FIELD_TYPE_STRING;
      }
    }

    return _FIELD_TYPE_NUMBER;
  }

  /**
  * Execute the query and the results
  * will be passed by reference to &$nrows and &$result
  *
  * @param string   $query
  * @param integer  $nrows
  * @param array    $result
  * @param boolean  $isDbg
  *
  */
  function execFetchQuery( &$query, &$nrows, &$result, $isDbg = _DEBUG_OPTION){

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    $s = OCIParse($this->dbConn, $query);
    $r = OCIExecute($s, OCI_DEFAULT);

    if($r == true){

      $nrows = oci_fetch_all($s, $result);

      $this->nrows  = $nrows;
      $this->result = $result;


      $this->status = true;

      oci_free_statement($s);
      return true;
    }else{

      $dbgInfo = debug_backtrace();
      if($isDbg){

        echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
      }

      $this->status = false;
      return false;
    }

  }

  /**
   * fetch the field' properties
   *
   * @param string  $query
   * @param array   $result
   * @param boolean $isDbg
   * @return booelan
   */
  function execFetchField( &$query, &$result, $isDbg = _DEBUG_OPTION){

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo  "<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    $s = OCIParse($this->dbConn, $query);
    $r = OCIExecute($s, OCI_DEFAULT);

    if($r == true){

      $result["nField"] = oci_num_fields($s, $result);

      for($iFld = 1; $iFld < $result["nField"] ; $iFld++){

        $result["name"][$iFld]      = oci_field_name($s, $iFld);

        $result["type"][$iFld]      = oci_field_type($s, $iFld);

        $result["easyType"][$iFld]  = $this->easyType($result["type"][$iFld]);
      }

      $this->status = true;

      oci_free_statement($s);

      return true;
    }else{

      $dbgInfo = debug_backtrace();
      if($isDbg){

        echo  "<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
      }

      $this->status = false;
      return false;
    }

  }

  /**
  * Exec query not a fetch type
  * it accomodates UPDATE, INSERT, DELETE, CREATE, MODIFY, etc
  * Which not returns any result of rows of data.
  *
  * @param string   $query
  * @param boolean  $isDbg true show the query,
                           false dont show.
  * @return boolean false when it fails
  *
  */
  function execQuery( $query,
                      $isDbg = _DEBUG_OPTION){

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    $s = OCIParse($this->dbConn, $query);
    $r = OCIExecute($s, OCI_DEFAULT);
    if($r == true){

      $this->status = true;
      return true;
    }else{

      $dbgInfo = debug_backtrace();
      if($isDbg){

        echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
      }

      $this->status = false;
      return false;
    }
  }

  /**
  *
  * @param void
  * @return array
  *
  */
  function result(){

    return $this->result;
  }

  /**
  *
  * @param void
  * @return integer
  *
  */
  function nrows(){

    return $this->nrows;
  }

  /**
  *
  * @param void
  * @return string
  *
  */
  function getStrConn(){

    return $this->dbConn;
  }

  /**
  *
  * @param void
  *
  */
  function commit(){

    if($this->status == true){

      OCICommit($this->dbConn);
    }
  }

  /**
  * get the status
  *
  * @param void
  * @return boolean
  *
  */
  function getStatus(){

    return $this->status;
  }

  /**
   * set Status manually
   *
   * @param boolean $status
   */
  function setStatus($status){

    $this->status = $status;
  }
}
?>